package com.example.demo1.filter;


import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @ClassName SignFilter
 * @Description TODO
 * @Author HDXYA
 * @Date 2020/4/7 19:52
 * @Version 1.0
 **/
public class SignFilter implements Filter {
    @Value("${sign.secret}")
    private String secret;

    @Value("${sign.open}")
    private String signOpen;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        StringBuffer url=request.getRequestURL();
        String queryString=request.getQueryString()==null?"":request.getQueryString();
        String sign=request.getParameter("sign");
        String secret="heduxing";
        String accessKey="feidaiApp";
        String md5Sign=DigestUtils.md5Hex(accessKey+secret+url).toUpperCase();
        if(!md5Sign.equals(sign)) {
           System.out.println("签名验证失败" + url);
           throw new RuntimeException("签名验证失败");

       }
        System.out.println("签名验证成功" + request.getRequestURI());
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
